package hdgeom.topomesh;

import processing.core.PVector;

public class MeshConstructor {
	public TopoMesh constructCube() {
		TopoMesh mesh = new TopoMesh();
		mesh.addNode(-0.5f, -0.5f, -0.5f);
		mesh.addNode(-0.5f, 0.5f, -0.5f);
		mesh.addNode(0.5f, 0.5f, -0.5f);
		mesh.addNode(0.5f, -0.5f, -0.5f);
		mesh.addNode(-0.5f, -0.5f, 0.5f);
		mesh.addNode(-0.5f, 0.5f, 0.5f);
		mesh.addNode(0.5f, 0.5f, 0.5f);
		mesh.addNode(0.5f, -0.5f, 0.5f);
		mesh.addQuad(3, 2, 1, 0);
		mesh.addQuad(0, 1, 5, 4);
		mesh.addQuad(1, 2, 6, 5);
		mesh.addQuad(2, 3, 7, 6);
		mesh.addQuad(3, 0, 4, 7);
		mesh.addQuad(4, 5, 6, 7);
		return mesh;
	}

	public void constructSphere(float radius, int u, int v) {
		TopoMesh mesh = new TopoMesh();
		TopoMeshNode[][] nodes = new TopoMeshNode[u][v];
		float dAngle1 = (float) Math.PI / (u + 1);
		float dAngle2 = 2 * (float) Math.PI / v;
		TopoMeshNode top = new TopoMeshNode(0, 0, radius);
		TopoMeshNode bottom = new TopoMeshNode(0, 0, -radius);
		mesh.addNode(top);
		mesh.addNode(bottom);
		for (int i = 1; i < u + 1; i++) {
			float z = radius * (float) Math.cos(dAngle1 * i);
			float r = radius * (float) Math.sin(dAngle1 * i);
			for (int j = 0; j < v; j++) {
				float x = r * (float) Math.cos(dAngle2 * j);
				float y = r * (float) Math.sin(dAngle2 * j);
				nodes[i - 1][j] = new TopoMeshNode(x, y, z);
				mesh.addNode(nodes[i - 1][j]);
			}
		}
		for (int i = 0; i < nodes.length - 1; i++) {
			for (int j = 0; j < nodes[i].length; j++) {
				mesh.addQuad(nodes[i][j], nodes[i][(j + 1) % nodes[i].length],
						nodes[i + 1][(j + 1) % nodes[i].length],
						nodes[i + 1][j]);
			}
		}
		for (int i = 0; i < nodes[0].length; i++) {
			mesh.addTri(nodes[0][i], nodes[0][(i + 1) % nodes[0].length], top);
			mesh.addTri(nodes[nodes.length - 1][i],
					nodes[nodes.length - 1][(i + 1) % nodes[0].length], bottom);
		}
	}
}
